<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="流程KEY" prop="key">
        <el-input
          v-model="queryParams.key"
          placeholder=""
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder=""
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属分类" prop="category">
        <el-input
          v-model="queryParams.category"
          placeholder=""
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
              type="primary"
              icon="el-icon-upload"
              size="small"
              @click="handleImport"
            >部署流程定义</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="definitionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="流程ID" prop="id" min-width="10%" />
      <el-table-column label="流程KEY" prop="key"  min-width="7%" />
      <el-table-column label="流程名称" prop="name" min-width="12%" />
      <el-table-column label="版本" prop="version" min-width="4%" />
      <el-table-column label="流程描述" prop="description" min-width="9%"/>
      <el-table-column label="所属分类" prop="category" min-width="11%"/>
      <el-table-column label="部署时间" prop="deploymentTime" min-width="8%">
        <template slot-scope="scope">
          <span>{{ moment(scope.row.deploymentTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="流程定义" :show-overflow-tooltip="true" min-width="16%"> 
        <template slot-scope="scope">
          <el-button
            size="small"
            type="text"
          >{{ scope.row.resourceName }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="流程图" :formatter="getDiagramResourceName" prop="diagramResourceName" min-width="15%" >
        <template slot-scope="scope">
          <el-button
            size="small"
            type="text"
          >{{ scope.row.diagramResourceName }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="流程定义状态" prop="suspendStateName" min-width="6%" >
        <template slot-scope="scope">
          <span style="color:#F00; font-weight:bold">{{ scope.row.suspendStateName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="30%">
        <template slot-scope="scope"> 
          <el-button
            size="small"
            type="primary"
            round
            icon="el-icon-user"
            @click="handleSuspendState(scope.row)"
          >{{ scope.row.suspendState === '2' ? '激活' : '挂起' }}</el-button>

          <el-button
            size="small"
            round
            type="info"
            icon="el-icon-sort"
            @click="handleConvert(scope.row)"
          >转模型</el-button>

          <el-button
            size="small"
            round
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 部署流程定义对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".bpmn, .zip, .bar"
        name="processDefinition"
        :headers="upload.headers"
        :action="upload.url"
        enctype="multipart/form-data"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“bpmn”、“zip”或“bar”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
  
</template>

<script>

import { listDefinition, removeDefinition, convertDefinition, suspendOrActiveDefinition} from "@/api/process/processMenu/definition";
import { getToken } from "@/utils/auth";

export default {
  name: "Model",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 表格数据
      definitionList: [],
       // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        key: undefined,
        category: undefined
      },
      // 流程导入参数
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/activiti/definition/upload"
      },
      // 图片显示参数
      detailImg: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "",
      },
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      
    };
  },
  created() {
    this.getList();
  },
  


  computed: {
  },
  methods: {
    getDiagramResourceName(row, column){
      return row.key+".png"
    },

    /** 查询流程定义列表 */
    getList() {
      this.loading = true;
      listDefinition(this.queryParams).then(response => {
          this.definitionList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    
    /** 转模型按钮操作 */
    handleConvert(row) {
      var params = new URLSearchParams();
      params.append('processDefinitionId', row.id);
      this.$confirm('是否转换成流程模型', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return convertDefinition(params);
        }).then(() => {
          this.msgSuccess("操作成功");
        }).catch(function() {});
    },

     /** 删除按钮操作 */
    handleDelete(row) {
      var params = new URLSearchParams();
      params.append('ids', row.deploymentId);
      this.$confirm('是否确认删除名称为"' + row.name + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return removeDefinition(params);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },

     /** 激活或挂起按钮操作 */
    handleSuspendState(row) {
      var applyState = row.suspendState === '2' ? '激活' : '挂起';
      var params = new URLSearchParams();
      params.append('id', row.id);
      params.append('suspendState', row.suspendState);
      this.$confirm("确认要" + applyState + "流程定义吗?", "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return suspendOrActiveDefinition(params);
        }).then(() => {
          this.getList();
          this.msgSuccess("操作成功");
        }).catch(function() {});
    },

    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "部署流程";
      this.upload.open = true;
    },
        // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.msgSuccess("操作成功");
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },

  }
};
</script>